package net.cjw.initsb.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;

import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.dao.RoleMapper;
import net.cjw.initsb.dao.RolePermissionConnectMapper;
import net.cjw.initsb.entity.Role;
import net.cjw.initsb.entity.RolePermissionConnect;
import net.cjw.initsb.service.RolePermissionServiceI;
import net.cjw.initsb.service.RoleServiceI;
import net.cjw.initsb.utils.UUIDBuild;

@Service
@Transactional
public class RoleServiceImpl implements RoleServiceI {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RolePermissionServiceI rolePermissionServiceI;
	@Autowired
	private RolePermissionConnectMapper rolePermissionConnectMapper;

	@Override
	public ServiceResult<Role> selectOne(Role entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ServiceResult<Object> selectAll(Integer start, Integer max) {
		PageHelper page = new PageHelper();
		page.startPage(start, max);
		List<Role> list = roleMapper.selectAll();
		PageInfo<Role> pageInfo = new PageInfo<Role>(list);
		return ServiceResult.buildSuccess(list, pageInfo.getTotal());
	}

	@Override
	public ServiceResult<String> create(Role entity) {
		Role rs = roleMapper.selectOne(entity);
		if (rs == null) {
			entity.setCreateTime(new Date());
			entity.setId(UUIDBuild.createUUId());
			roleMapper.insert(entity);
			return ServiceResult.buildSuccess();
		}

		return ServiceResult.buildError("角色已存在");
	}

	@Override
	public ServiceResult<String> create(Role entity, String permissionIds) {
		// 检查
		Role tmp;
		Role rs;
		tmp = new Role();
		tmp.setRoleName(entity.getRoleName());
		rs = roleMapper.selectOne(tmp);
		if (rs != null) {
			return ServiceResult.buildError("角色已存在");
		}
		tmp = new Role();
		tmp.setRoleValue(entity.getRoleValue());
		rs = roleMapper.selectOne(tmp);
		if (rs != null) {
			return ServiceResult.buildError("角色标识值以存在");
		}

		// 插入
		String rid = null;
		entity.setCreateTime(new Date());
		rid = UUIDBuild.createUUId();
		entity.setId(rid);
		roleMapper.insert(entity);
		// 绑定权限
		if (!Strings.isNullOrEmpty(permissionIds)) {
			String[] idArr = permissionIds.split(",");
			for (String pid : idArr) {
				RolePermissionConnect rp = new RolePermissionConnect();
				rp.setPermissionId(pid);
				rp.setRoleId(rid);
				rolePermissionServiceI.create(rp);
			}
		}

		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<String> update(Role entity) {
		Role find;
		find = new Role();
		find.setRoleName(entity.getRoleName());
		find = roleMapper.selectOne(find);
		if (find != null) {
			return ServiceResult.buildError("角色名称已被使用");
		}
		roleMapper.updateByPrimaryKeySelective(entity);
		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<String> delete(Role entity) {
		Role tmp = roleMapper.selectOne(entity);
		if (tmp == null) {
			return ServiceResult.buildError("角色不存在");
		}
		int i = roleMapper.delete(entity);
		if (i == 0) {
			return ServiceResult.buildError("删除失败");
		}
		// 删除对应的权限绑定
		RolePermissionConnect rp = new RolePermissionConnect();
		rp.setRoleId(tmp.getId());
		rolePermissionServiceI.delete(rp);
		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<String> update(Role entity, String permissionIds) {
		Role tmp;
		Role rs;
		tmp = new Role();
		tmp.setRoleName(entity.getRoleName());
		rs = roleMapper.selectOne(tmp);
		if (rs != null && !rs.getRoleName().equals(entity.getRoleName())) {
			return ServiceResult.buildError("角色名称已被使用");
		}
		tmp = new Role();
		tmp.setRoleValue(entity.getRoleValue());
		rs = roleMapper.selectOne(tmp);
		if (rs != null && !rs.getRoleValue().equals(entity.getRoleValue())) {
			return ServiceResult.buildError("角色标识值已被使用");
		}
		roleMapper.updateByPrimaryKeySelective(entity);
		// 重新绑定权限
		// 删除对应的权限绑定
		RolePermissionConnect rp = new RolePermissionConnect();
		rp.setRoleId(entity.getId());
		rolePermissionServiceI.delete(rp);
		// 绑定权限
		if (!Strings.isNullOrEmpty(permissionIds)) {
			String[] idArr = permissionIds.split(",");
			for (String pid : idArr) {
				rp = new RolePermissionConnect();
				rp.setPermissionId(pid);
				rp.setRoleId(entity.getId());
				rolePermissionServiceI.create(rp);
			}
		}
		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<List<RolePermissionConnect>> findRolePermission(String id) {

		RolePermissionConnect rp = new RolePermissionConnect();
		rp.setRoleId(id);
		;
		return ServiceResult.buildSuccess(rolePermissionConnectMapper.select(rp));
	}

}
